package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.SaveAllToESService;
import com.qingcheng.service.goods.SkuService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 将数据库中的sku商品信息数据,全部映射到ES中去
 */
@Service
public class SaveAllToESServiceImpl implements SaveAllToESService {

    @Autowired
    private SkuService skuService;

    public void saveAllToESImpl() throws Exception{
        //连接rest接口
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(http);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
        boolean flag = true;
        int i = 90810 / 10000 + 1;
        int j = 1;
        while (flag) {
            if (j == i) {
                flag = false;
            }
            PageResult<Sku> page = skuService.findPage(j++, 10000);
            List<Sku> rows = page.getRows();
            BulkRequest bulkRequest = new BulkRequest();//桶,打包request请求,可以看成是一个List容器
            for (Sku sku : rows) {
                IndexRequest indexRequest = new IndexRequest("sku", "doc", sku.getId());

                Map skuMap = new HashMap();
                Map spec = new HashMap();
                skuMap.put("name", sku.getName());
                skuMap.put("price", sku.getPrice());
                skuMap.put("image", sku.getImage());
                skuMap.put("createTime",sku.getCreateTime());
                skuMap.put("spuId",sku.getId());
                skuMap.put("categoryName", sku.getCategoryName());
                skuMap.put("brandName", sku.getBrandName());
                skuMap.put("spec",sku.getSpec());
                skuMap.put("saleNum", sku.getSaleNum());
                skuMap.put("commentNum", sku.getCommentNum());
                //{'颜色': '红色', '版本': '8GB+128GB'}
                Map<String, Object> map = (Map) JSON.parseObject(sku.getSpec(), Map.class);
                for (String key : map.keySet()) {
                    spec.put(key, map.get(key));
                }
                skuMap.put("spec", spec);

                indexRequest.source(skuMap);

                bulkRequest.add(indexRequest);

            }
            //获取响应结果
            BulkResponse responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
            int status = responses.status().getStatus();
            System.out.println(status);

        }

        restHighLevelClient.close();

    }

}
